import 'package:app/api/account/index.dart';
import 'package:app/components/index.dart';
import 'package:app/util/StringUtils.dart';
import 'package:flutter/material.dart';

/// 修改密码
class Password extends StatefulWidget{

  const Password({super.key});

  @override
  State<StatefulWidget> createState()  => _PasswordState();
}

class _PasswordState extends State<Password>{

  final GlobalKey<FormState>? _formKey = GlobalKey<FormState>();

  String _password = "";

  String _newPassword = "";

  void hideKeyBorder() {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus?.unfocus();
    }
  }

  /// 保存
  void submit(){
    Forms.validate(_formKey!).then((valid) {
      if (valid) {
        if(_password != _newPassword){
          Error.of(context).show('两次密码不一致');
          return;
        }
        Account.of(context).modifyPassword(_newPassword).then((res){
          if(StringUtils.isNotNull(res)){
            clear();
            Success.of(context).show('密码修改成功');
          }
        });
      }
    });
  }

  /// 设置值
  void setValue(String key, String value){
    switch(key){
      case '_password' : setState(() => _password = value);break;
      case '_newPassword' : setState(() => _newPassword = value);break;
      default:
        throw Exception("key error");
    }
  }

  /// 清空
  void clear(){
    setState(() {
      _password = "";
      _newPassword = "";
    });
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: AppBars(context: context, title: '修改密码',),
        body: GestureDetector(
          onTap: hideKeyBorder,
          child: Column(
            children: [
              Group(
                // title: '密码',
                child: Forms(
                    formKey: _formKey,
                    formItem: [
                      FormItem(
                        type: 'inputPassword',
                        labelWidth: 90,
                        labelText: '密    码',
                        placeholder: '请输入密码',
                        filedName: 'password',
                        value: _password,
                        icon: const Icon(Icons.lock),
                        onChange: (value) => setValue('_password', value),
                        rules: const {'password': [{'required': true, 'message': '密码不能为空'}]},
                      ),
                      FormItem(
                        type: 'inputPassword',
                        labelWidth: 90,
                        labelText: '新密码',
                        value: _newPassword,
                        placeholder: '请输入新密码',
                        filedName: 'newPassword',
                        onChange: (value) => setValue('_newPassword', value),
                        icon: const Icon(Icons.lock),
                        rules: const {'newPassword': [{'required': true, 'message': '新密码不能为空'}]},
                      ),
                    ]
                ),
              ),
              SizedBox(
                width: MediaQuery.of(context).size.width,
                child: Padding(
                  padding: const EdgeInsets.only(top: 20, left: 20, right: 20),
                  child: Button(
                    onPressed: submit,
                    label: '确认',
                    height: 60,
                    fontSize: 18,
                  ),
                ),
              ),
            ],
          )
        )
    );
  }

}
